package com.shenmazong.bootdemotask.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
@EnableScheduling   // 1.开启定时任务
@EnableAsync        // 2.开启多线程
public class WorkTask {

    /**
     * TODO 每秒钟执行一次任务
     */
    @Async
    @Scheduled(fixedRate = 1000)
    public void fixRateTask() {
        String strDateTime = new Date().toString();

        try {
            Thread.sleep(1000*30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName()+"fixrate:"+strDateTime);
    }

    /**
     * TODO 每5秒执行一次任务
     */
    @Scheduled(cron = "0/5 * * * * *")
    public void fixTimeTask() {
//        String strDateTime = new Date().toString();
//
//        try {
//            Thread.sleep(1000*10);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//        System.out.println(Thread.currentThread().getName()+"-fixtime:"+strDateTime);
    }
}
